﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkSchedueSoft.Data;

namespace WorkSchedueSoft
{
    public partial class CommandWin : Form
    {
        EditWin editwin;

        SchedueCommandItem command_item;

        PopWinMode current_command_win;

        public CommandWin()
        {
            InitializeComponent();

            APPManager.Instance.Command_win = this;

            InitData();
        }

        public void InitData()
        {
            current_command_win = APPManager.Instance.current_commandwin_mode;
            command_item = DataManager.Instance.Current_command_item;
            editwin = APPManager.Instance.Edit_win;
            editwin.Enabled = false;

            this.Text = APPManager.Instance.current_commandwin_mode == PopWinMode.ADD ? "添加命令" : "编辑命令";

            var current_command_info = DataManager.Instance.Current_command_item;
            if (current_command_info != null)
            {
                name_textbox.Text = current_command_info.name;
                path_textbox.Text = current_command_info.path;
                para_textbox.Text = current_command_info.boot_para;
                description_textbox.Text = current_command_info.description;
                wait_radioButton.Checked = current_command_info.tip_for_complete;
                delay_textBox.Text = current_command_info.delay.ToString() ;
                startSrctextBox.Text = current_command_info.boot_source_path;
            }


        }

        private void CommandWin_Load(object sender, EventArgs e)
        {

        }

        private void okbutton_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(name_textbox.Text)) return;

            var current_command_info = DataManager.Instance.Current_command_item;
            current_command_info.name = name_textbox.Text;
            current_command_info.path = path_textbox.Text;
            current_command_info.boot_para = para_textbox.Text;
            current_command_info.description = description_textbox.Text;
            current_command_info.tip_for_complete = wait_radioButton.Checked;
            current_command_info.boot_source_path = startSrctextBox.Text;

            int delay;
            if (int.TryParse(delay_textBox.Text, out delay))
            {
                current_command_info.delay = delay;
            }

            if (current_command_win == PopWinMode.ADD)
            {

                Dictionary<string, SchedueCommandItem> t = APPManager.Instance.Edit_win.dict_schedue_command;

                if (!t.ContainsKey(command_item.name))
                {
                    editwin.schedueitem.commands.Add(command_item);
                }
                else
                {
                    MessageBox.Show("已经存在一个同名的了");
                    return;
                }

            }
            else if (current_command_win == PopWinMode.EDIT)
            {
                
            }

            editwin.RefreshData();
            this.Close();

        }

        private void cancelbutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void getfilepath_button_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.path_textbox.Text = this.openFileDialog1.FileName;
            }
        }

        private void path_textbox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;                                                              //重要代码：表明是所有类型的数据，比如文件路径
            else
                e.Effect = DragDropEffects.None;
        }

        private void path_textbox_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();       //获得路径
            path_textbox.Text = path;
        }

        private void startSrctextBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;                                                              //重要代码：表明是所有类型的数据，比如文件路径
            else
                e.Effect = DragDropEffects.None;
        }

        private void startSrctextBox_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();       //获得路径
            startSrctextBox.Text = path;
        }

        private void programeStartGetfilebutton_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.startSrctextBox.Text = this.openFileDialog1.FileName;
            }
        }
    }
}
